﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:personalplaner.common.controls"
                    xmlns:conv="clr-namespace:personalplaner.common.converters">

    <!--<Style TargetType="{x:Type Border}" x:Key="border">
        <Setter Property="Height" Value="14"/>
        <Setter Property="Width" Value="14"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Margin" Value="1"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
    </Style>-->

    <conv:ColorToSolidColorBrushConverter x:Key="ColorToSolidColorBrushConverter"/>
    <conv:BoolInvertConverter x:Key="InverseBoolConverter"/>

    <Style x:Key="ColorPickerToggleButtonStyle" TargetType="ToggleButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid SnapsToDevicePixels="True">
                        <!--<chrome:ButtonChrome x:Name="ToggleButtonChrome"
                                            RenderChecked="{Binding IsOpen, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ColorPicker}}"
                                            RenderEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ColorPicker}}"
											RenderMouseOver="{TemplateBinding IsMouseOver}"
											RenderPressed="{TemplateBinding IsPressed}" />-->

                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            
                            <Border Background="Transparent" Grid.ColumnSpan="2"/>

                            <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />

                            <Grid x:Name="arrowGlyph" IsHitTestVisible="False" Grid.Column="1" Margin="5">
                                <Path Width="7" Height="4" Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z" Fill="#FF000000"/>
                            </Grid>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <DrawingBrush  x:Key="CheckerBrush" Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="White">
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="0,0 100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="LightGray">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry Rect="0,0 50,50" />
                            <RectangleGeometry Rect="50,50 50,50" />
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
    
    <DataTemplate x:Key="ColorItemTemplate">
        <Grid>
            <Border BorderBrush="#FFC9CACA" BorderThickness="1" Margin="2,2,2,2" >
                <Rectangle Width="14" Height="14">
                    <Rectangle.Style>
                        <Style TargetType="Rectangle">
                            <Setter Property="Fill" Value="{Binding Color, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Name}" Value="Transparent">
                                    <Setter Property="Fill" Value="{StaticResource CheckerBrush}" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Rectangle.Style>
                </Rectangle>
            </Border>
        </Grid>
    </DataTemplate>

    <Style x:Key="ColorPaletteLisBoxStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="ToolTip" Value="{Binding Name}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid ToolTip="{Binding Name}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        <Border BorderThickness="1" Background="Transparent" BorderBrush="Transparent" x:Name="_outerBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <Border Background="Transparent" BorderThickness="1" BorderBrush="Transparent" x:Name="_innerBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="_outerBorder" Property="BorderBrush" Value="#FFFF0000" />
                            <Setter TargetName="_innerBorder" Property="BorderBrush" Value="#FFFFFF00" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="_outerBorder" Property="BorderBrush" Value="#FFFF0000" />
                            <Setter TargetName="_innerBorder" Property="BorderBrush" Value="#FFFFFF00" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ColorDisplayStyle" TargetType="Border">
        <Setter Property="Background" Value="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}" Value="Transparent">
                <Setter Property="Background" Value="{StaticResource CheckerBrush}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <LinearGradientBrush x:Key="PopupBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
        <LinearGradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Offset="0" Color="#FFffffff"/>
                <GradientStop Offset="1" Color="#FFE8EBED"/>
            </GradientStopCollection>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="ColorPickerDarkBorderBrush" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FFA3AEB9" Offset="0"/>
        <GradientStop Color="#FF8399A9" Offset="0.375"/>
        <GradientStop Color="#FF718597" Offset="0.375"/>
        <GradientStop Color="#FF617584" Offset="1"/>
    </LinearGradientBrush>
    
    <!-- 
    *****************************************************************
    Template for ClosableTabItemHeader
    *****************************************************************
    -->
    <Style TargetType="{x:Type local:ColorPicker}">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>

        <Setter Property="ButtonStyle" Value="{StaticResource ColorPickerToggleButtonStyle}" />
        
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type  local:ColorPicker}" >

                    <Grid>
                        <Border BorderBrush="#FF707070" BorderThickness="1" CornerRadius="2" SnapsToDevicePixels="True">
                            <ToggleButton x:Name="PART_ColorPickerToggleButton" IsTabStop="True" MinHeight="22" IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                      IsHitTestVisible="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
                                      Style="{TemplateBinding ButtonStyle}">
                                <Grid Margin="2,2,0,2">
                                    <Border x:Name="ColorOnly" Style="{StaticResource ColorDisplayStyle}" Margin="2"/>
                                    
                                    <Border x:Name="ColorAndName" Background="White" Visibility="Hidden">
                                        <StackPanel Orientation="Horizontal">
                                            <Border HorizontalAlignment="Left" Width="20" Margin="2,1,4,1" Style="{StaticResource ColorDisplayStyle}" BorderThickness="1" BorderBrush="#FFC9CACA" />
                                            <TextBlock Text="{Binding SelectedColorText, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Center" />
                                        </StackPanel>
                                    </Border>
                                </Grid>
                            </ToggleButton>
                        </Border>

                        <Popup x:Name="PART_ColorPickerPalettePopup"  VerticalAlignment="Bottom" IsOpen="{Binding ElementName=PART_ColorPickerToggleButton, Path=IsChecked}" StaysOpen="False" AllowsTransparency="True" Focusable="False"  HorizontalOffset="1" VerticalOffset="1"  >
                            <Border BorderThickness="1" Background="{StaticResource PopupBackgroundBrush}" BorderBrush="{StaticResource ColorPickerDarkBorderBrush}" Padding="3">

                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>

                                    <TextBlock Text="Standard"/>
                                    <ListBox x:Name="PART_StandardColors" Grid.Row="1" SelectionMode="Single" Background="Transparent" BorderThickness="0"
                                                         ItemsSource="{Binding StandardColors, RelativeSource={RelativeSource TemplatedParent}}"                                                      
                                                         ItemTemplate="{StaticResource ColorItemTemplate}"
                                                         ItemContainerStyle="{StaticResource ColorPaletteLisBoxStyle}"
                                                         Style="{x:Null}">
                                        <ListBox.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <WrapPanel Width="200" />
                                            </ItemsPanelTemplate>
                                        </ListBox.ItemsPanel>
                                    </ListBox>

                                    <TextBlock Text="Available" Grid.Row="2"/>
                                    <ListBox x:Name="PART_AvailableColors" Grid.Row="3" Background="Transparent" BorderThickness="0" SelectionMode="Single"
                                                         ItemsSource="{Binding AvailableColors, RelativeSource={RelativeSource TemplatedParent}}"                                                      
                                                         ItemTemplate="{StaticResource ColorItemTemplate}"
                                                         ItemContainerStyle="{StaticResource ColorPaletteLisBoxStyle}"
                                                         Style="{x:Null}">
                                        <ListBox.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <WrapPanel Width="200" />
                                            </ItemsPanelTemplate>
                                        </ListBox.ItemsPanel>
                                    </ListBox>
                                </Grid>
                            </Border>
                        </Popup>
                    </Grid>

                    <!--<Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <StackPanel Orientation="Horizontal">
                            <Border Background="White" Style="{StaticResource border}" Padding="1">
                                <Path Stroke="Red" StrokeThickness="2" Data="M 10,1 L 1,10" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <Border Background="White" Style="{StaticResource border}"/>
                            <Border Background="Black" Style="{StaticResource border}"/>
                            <Border Background="Gray" Style="{StaticResource border}"/>
                            <Border Background="LightGray" Style="{StaticResource border}"/>
                            <Border Background="WhiteSmoke" Style="{StaticResource border}"/>
                            <Border Background="Orange" Style="{StaticResource border}"/>
                        </StackPanel>

                        <StackPanel Orientation="Horizontal" Grid.Row="1">
                            <Border Background="Red" Style="{StaticResource border}"/>
                            <Border Background="Green" Style="{StaticResource border}"/>
                            <Border Background="Blue" Style="{StaticResource border}"/>
                            <Border Background="Yellow" Style="{StaticResource border}"/>
                            <Border Background="YellowGreen" Style="{StaticResource border}"/>
                            <Border Background="Purple" Style="{StaticResource border}"/>
                            
                        </StackPanel>

                    </Grid>-->
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>